//
//  FindViewController.m
//  Project_A Gary
//
//  Created by leilei on 16/3/24.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "FindViewController.h"
#import "AppDelegate.h"
#import "FindTableViewCell.h"
#import "DrawerViewController.h"

@interface FindViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableV;
@property (nonatomic, retain)NSMutableArray *dataArr;

// 创建一个抽屉View
@property (nonatomic, retain)DrawerViewController *drawerVC;

// 右侧索引栏的数组
@property (nonatomic, retain)NSMutableArray *sectionArr;

// 在出现抽屉的时候出现一个透明的view为了只轻拍他
@property (nonatomic, retain)UIView *alphaView;

// 小菊花属性
@property (nonatomic, retain)MBProgressHUD *MBView;

@end

@implementation FindViewController

-(void)dealloc
{
    [_tableV release];
    [_dataArr release];
    [_drawerVC release];
    [_sectionArr release];
    [_alphaView release];
    [_MBView release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationItem.title = @"找车";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:32 / 255.0 green:105 / 255.0 blue:236 / 255.0 alpha:1];
    
    [self createTableView];
    [self dataHandle];
    
   

    self.alphaView = [[UIView alloc] init];
    self.alphaView.backgroundColor = [UIColor grayColor];
    self.alphaView.alpha = 0.5;
    [self.view addSubview:self.alphaView];
    [_alphaView release];
    
    
    [self createrDrawerVC];
    [self createTap];
    
    
    [self createMBView];
}




#pragma mark - 设置抽屉页面
- (void)createrDrawerVC {
    self.drawerVC = [[DrawerViewController alloc] init];
    self.drawerVC.view.frame = CGRectMake(BOUNDS_WIDTH + 300, 64 + 30, 300, BOUNDS_HEIGHT - 64 - 49 - 60 );
    // 将抽屉设置为子视图控制器
    [self addChildViewController:self.drawerVC];
    [self.view addSubview:self.drawerVC.view];
    [_drawerVC release];
}

#pragma mark - 设置轻拍手势(轻拍的时候可以撤回抽屉)
- (void)createTap
{
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
    [self.alphaView addGestureRecognizer:tapGR];
    //tapGR.numberOfTapsRequired = 2;
    [tapGR release];
}


#pragma mark - 实现收回抽屉的方法
- (void)tapAction: (UITapGestureRecognizer *)tap
{

        [UIView animateWithDuration:1 animations:^{
            //self.tableV.frame = CGRectMake(0, 64, BOUNDS_WIDTH, BOUNDS_HEIGHT);
            self.alphaView.frame = CGRectZero;

            self.drawerVC.view.frame = CGRectMake(BOUNDS_WIDTH + 300, 64 + 30, 300, BOUNDS_HEIGHT - 64 - 49 - 60);
    
        }];


    
}

#pragma mark - 点击tableViewCell 出现抽屉(传值id到抽屉页面上)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    
    
    // 点击cell 传值id
    //DrawerViewController *draVC = [[DrawerViewController alloc] init];
    
    self.drawerVC.drawerId = self.dataArr[indexPath.section][@"brands"][indexPath.row][@"id"];
//    [self.navigationController pushViewController:draVC animated:YES];
    
    // 点击cell 通过动画出现抽屉
    [UIView animateWithDuration:0.5 animations:^{
        
        //在出现抽屉的时候 初始化这个为了轻拍用的View
        
        self.alphaView.frame = CGRectMake(0, 0, BOUNDS_WIDTH , BOUNDS_HEIGHT );
        
        
        //self.tableV.frame = CGRectMake(0, 64, BOUNDS_HEIGHT, BOUNDS_HEIGHT);
        
        self.drawerVC.view.frame = CGRectMake(BOUNDS_WIDTH - 300, 64 + 30, 300, BOUNDS_HEIGHT - 64 - 49 - 60);
        
        
    }];
    
    
}









#pragma mark - 设置分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}

#pragma mark - 分区头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.dataArr[section][@"index"];
}



#pragma mark - 设置右侧索引栏
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.sectionArr;
}



#pragma mark - 必写的两个协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArr[section][@"rowNum"] integerValue];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FindTableViewCell" forIndexPath:indexPath];
    cell.name.text = self.dataArr[indexPath.section][@"brands"][indexPath.row][@"name"];
    [cell.logo sd_setImageWithURL:[NSURL URLWithString:self.dataArr[indexPath.section][@"brands"][indexPath.row][@"logo"]]];
    
    
    
    return cell;
}


#pragma mark - 解析数据主tableView
- (void)dataHandle
{
    NSString *str = @"http://mrobot.pcauto.com.cn/v2/price/brands?v=4.5.0";
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        
        
        self.dataArr = responseObject[@"sections"];
        // 建立一个section的数组 将dataArr里面每个字典中index对应的string 放到section数组里面
        self.sectionArr = [NSMutableArray array];
        for (NSDictionary *sectionDic in self.dataArr) {
            [self.sectionArr addObject:sectionDic[@"index"]];
            
        }
        // 加载数据以后 隐藏小菊花
        [self.MBView hide:YES];
        
        [_tableV reloadData];
    } filed:^(NSError *error) {
        
    }];
}


#pragma mark - 创建tableView
- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, BOUNDS_WIDTH, BOUNDS_HEIGHT) style:UITableViewStylePlain];
    self.tableV.backgroundColor = [UIColor whiteColor];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    self.tableV.rowHeight = 80;
    [_tableV release];
    [self.tableV registerClass:[FindTableViewCell class] forCellReuseIdentifier:@"FindTableViewCell"];
    
}

#pragma mark - 创建加载时候的小菊花
- (void)createMBView
{
    self.MBView = [[MBProgressHUD alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [self.view addSubview:self.MBView];
    // 显示小菊花
    [self.MBView show:YES];
    [_MBView release];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
